﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Application;
using Business;
using System.Web.UI.HtmlControls;

namespace UserControls
{
    public partial class AddParagraph : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                foreach (string key in Session.Keys)
                {
                    try
                    {
                        VOParagraph paragraphAdded = (VOParagraph)Session[key];
                        AddNewParagraphControl(paragraphAdded);
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }
            else
            {
                Session.RemoveAll();
            }
        }

        private void AddNewParagraphControl(VOParagraph paragraph)
        {
            Paragraph paragraphCtrl = (Paragraph)this.LoadControl("~/UserControls/Paragraph.ascx");
            paragraphCtrl.ParagraphID = paragraph.ID;
            paragraphCtrl.Title = paragraph.Title;
            paragraphCtrl.Content = paragraph.Content;
            
            updatePanelAddParagraph.ContentTemplateContainer.Controls.Add(paragraphCtrl);
            paragraphCtrl.UpdateEditParagraphPanel();
        }

        protected void add_Click(object sender, EventArgs e)
        {
            System.Threading.Thread.Sleep(3000);
            VOParagraph paragraph = new VOParagraph();
            paragraph.Title = txtTitle.Text;
            paragraph.Content = txtDesc.Text;
            txtTitle.Text = string.Empty;
            txtDesc.Text = string.Empty;
            //BusinessParagraph.AddParagraph(paragraph);
            AddNewParagraphControl(paragraph);
            updatePanelAddParagraph.Update();

            // TO DO: METTRE ID A LA PLACE
            string viewStateKey = paragraph.Title;
            Session[viewStateKey] = paragraph;
        }
    }
}